<?php
class UtilityController extends AppController {
    
    public $uses = array('Flavor','User','Category');
    public $components = array('RequestHandler');
    public $helpers = array('Js');
    
    
    public function index() {

    }
    
    /***
     * Category section
     */
    public function category() {
        
        $join = array(array('table'=>'categories',
                                     'type'=>'left',
                                     'alias'=>'b',
                                     'conditions'=>'Category.parent_id = b.id'));
        if($this->request->data){
            $condition = array();
            if($this->request->data['search']['category_name'] != ''){
                $condition['Category.category like'] = '%'.$this->request->data['search']['category_name'].'%';
            }
            
            if($this->request->data['search']['parent_id'] != ''){
                $condition['Category.parent_id ='] = $this->request->data['search']['parent_id'];
            }
            
            $this->paginate = array(
                'conditions' => $condition,
                'limit' => Configure::read('limit'),
                'order' => array('id' => 'asc'),
                'joins'=> $join
            );
        }else{
            $this->paginate = array('fields'=>array('Category.*','b.category'),'limit' => Configure::read('limit'),'order' => array('id' => 'asc'),'joins'=>$join);
        }
        
        
        $data = $this->paginate('Category');
        
        $this->set('title','Category');
        $this->set('subtitle','Control panel');
        $this->set('data',$data);
        $this->set('parent',$this->Category->find('list',array('fields'=>array('Category.id','Category.category'))));
    }
    
    public function categorymanagement() {
        $this->set('title','Category');
        $this->set('subtitle','Control panel');
    }
    
    public function categoryopr() {
        
        if($this->request->data){
            $this->Category->set($this->request->data);
            if ($this->Category->validates()) {
                $data =  $this->Category->save($this->request->data);
                $this->Session->setFlash(__('Record successfully saved.'),'success');
                $this->redirect(array('controller'=>'utility','action'=>'category'));
            } else {
                $errors = $this->Category->validationErrors;
            }
        }
        
        if(isset($_GET['action'])){
            $opr = unserialize(base64_decode($_GET['action']));
            switch($opr['opr']){
                case 'edit' :
                    $data = $this->Category->find('first',array('conditions'=>array('Category.id'=>$opr['id'])));
                    if($data){
                        $this->request->data = $data;
                    }
                    break;
                case 'delete' :
                    $data = $this->Category->delete($opr['id']);
                    if($data){
                        $this->Session->setFlash(__('Record successfully deleted.'),'success');
                        $this->redirect(array('controller'=>'utility','action'=>'category'));
                    }
                    break;
            }
        }
        
        
        $this->set('title','Category');
        $this->set('subtitle','Control panel');
        $this->set('parent',$this->Category->find('list',array('fields'=>array('Category.id','Category.category'))));
    }
    
    /***
     * Transcode section
     */
    public function flavor() {
        
        if($this->request->data){
            
            $condition = array();
            if($this->request->data['search']['device_name'] != ''){
                $condition['Flavor.device_name like'] = '%'.$this->request->data['search']['device_name'].'%';
            }
            
            $this->paginate = array(
                'conditions' => $condition,
                'limit' => Configure::read('limit'),
                'order' => array('id' => 'asc')
            );
        }else{
            $this->paginate = array('limit' => Configure::read('limit'),'order' => array('id' => 'asc'));
        }
        
        $data = $this->paginate('Flavor');
        $this->set('title','Transcode');
        $this->set('subtitle','Control panel');
        $this->set('data',$data);
        $this->set('device',array('Mastering'=>'Mastering','Desktop'=>'Desktop','IOS'=>'IOS','Andriod'=>'Andriod'));
    }
    
    public function flavoropr() {
        
        if($this->request->data){
            $this->Flavor->set($this->request->data);
            if ($this->Flavor->validates()) {
                $data =  $this->Flavor->save($this->request->data);
                $this->Session->setFlash(__('Record successfully saved.'),'success');
                $this->redirect(array('controller'=>'utility','action'=>'flavor'));
            } else {
                $errors = $this->Flavor->validationErrors;
            }
        }
        
        if(isset($_GET['action'])){
            $opr = unserialize(base64_decode($_GET['action']));
            switch($opr['opr']){
                case 'edit' :
                    $data = $this->Flavor->find('first',array('conditions'=>array('Flavor.id'=>$opr['id'])));
                    if($data){
                        $this->request->data = $data;
                    }
                    break;
                case 'delete' :
                    $data = $this->Flavor->delete($opr['id']);
                    if($data){
                        $this->Session->setFlash(__('Record successfully deleted.'),'success');
                        $this->redirect(array('controller'=>'utility','action'=>'flavor'));
                    }
                    break;
            }
        }
        
        $this->set('title','Flavor');
        $this->set('subtitle','Control panel');
        $this->set('device',array('Mastering'=>'Mastering','Desktop'=>'Desktop','IOS'=>'IOS','Andriod'=>'Andriod'));
    }
}